import {
  BookModel
} from "../../models/book.js";
import {
  ClassicModel
} from "../../models/classic.js";

var bookModel = new BookModel();
var classicModel = new ClassicModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: false,
    userInfo: null,
    bookCount: 0,
    classics: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.userAuthorized();
    this.getMyBookCount();
    this.getMyFavor();
  },

  getMyFavor: function() {
    classicModel.getMyFavor(res => {
      this.setData({
        classics: res.data
      })
    })
  },

  getMyBookCount: function(event) {
    bookModel.getMyBookCount()
      .then(res => {
        this.setData({
          bookCount: res.data.count
        })
      })
  },

  onGetUserInfo: function(event) {
    let userInfo = event.detail.userInfo
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo
      })
    }
  },

  userAuthorized: function() {
    wx.getSetting({
      success: data => {
        if (data.authSetting["scope.userInfo"]) {
          wx.getUserInfo({
            success: res => {
              this.setData({
                hasUserInfo: true,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

  onJumpToAbout: function(event) {
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },

  onStudy: function() {
    wx.navigateTo({
      url: '/pages/course/course',
    })
  },

})